/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author tho.hoang
 */
public class TextItem extends ADetailItem {
    private int align = 1;

    public TextItem(String id, String title, String text) {
        super(id, title, text);
    }

    public TextItem(String id, String title, String text, int align) {
        super(id, title, text);
        this.align = align;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }

        initItem();

        int i = 0, tempX = 0, tempY = 0;
        if (align != ALIGN_LEFT && align != ALIGN_CENTER && align != ALIGN_RIGHT) {
            align = ALIGN_LEFT;
        }

        if (align == ALIGN_LEFT) {
            // paint title
            if (title.length() > 0) {
                tempX = getX() + style.getPadding().getLeft() + titleStyle.getPadding().getLeft();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getPadding().getTop();
                for (i = 0; i < titleRows.length; i++) {
                    font.getFontBold().drawString(g, titleRows[i], tempX, tempY + i * (font.getFontBold().getHeight() + titleStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
                }
            }

            // paint value
            if (value.length() > 0) {
                tempX = getX() + style.getPadding().getLeft() + valueStyle.getPadding().getLeft();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getHeight() + valueStyle.getPadding().getTop();
                for (i = 0; i < valueRow.length; i++) {
                    font.getFontNormal().drawString(g, valueRow[i], tempX, tempY + i * (font.getFontNormal().getHeight() + valueStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
                }
            }
        } else if (align == ALIGN_RIGHT) {
            // paint title
            if (title.length() > 0) {
                tempX = width - style.getPadding().getRight() - titleStyle.getPadding().getRight();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getPadding().getTop();
                for (i = 0; i < titleRows.length; i++) {
                    font.getFontBold().drawString(g, titleRows[i], tempX, tempY + i * (font.getFontBold().getHeight() + titleStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
                }
            }

            // paint value
            if (value.length() > 0) {
                tempX = width - style.getPadding().getRight() - valueStyle.getPadding().getRight();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getHeight() + valueStyle.getPadding().getTop();
                for (i = 0; i < valueRow.length; i++) {
                    font.getFontNormal().drawString(g, valueRow[i], tempX, tempY + i * (font.getFontNormal().getHeight() + valueStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
                }
            }
        } else if (align == ALIGN_CENTER) {
            // paint title
            if (title.length() > 0) {
                tempX = width / 2 + style.getPadding().getLeft();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getPadding().getTop();
                for (i = 0; i < titleRows.length; i++) {
                    font.getFontBold().drawString(g, titleRows[i], tempX, tempY + i * (font.getFontBold().getHeight() + titleStyle.getRowHeight()), Graphics.TOP | Graphics.RIGHT);
                }
            }

            // paint value
            if (value.length() > 0) {
                tempX = width / 2 + style.getPadding().getLeft();
                tempY = getY() + style.getPadding().getTop() + titleStyle.getHeight() + valueStyle.getPadding().getTop();
                for (i = 0; i < valueRow.length; i++) {
                    font.getFontNormal().drawString(g, valueRow[i], tempX, tempY + i * (font.getFontNormal().getHeight() + valueStyle.getRowHeight()), Graphics.TOP | Graphics.RIGHT);
                }
            }
        }
    }

    public int getAlign() {
        return align;
    }

    public void setAlign(int align) {
        this.align = align;
    }
}
